package com.firewolf.rocketmq;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

/**
 * 作者：刘兴 时间:2019-07-11 异步发送，通过回调函数来处理发送的结果
 **/
public class AsyncProducer {

  public static void main(String[] args) throws Exception {
    DefaultMQProducer mqProducer = new DefaultMQProducer("producer-group1");

    mqProducer.setNamesrvAddr("10.211.55.6:9876");

    mqProducer.start();

    for (int i = 0; i < 10; i++) {
      Message message = new Message("mytopic", "TagN", ("liuxing" + i).getBytes("UTF-8"));
      mqProducer.send(message,
          //回调函数，用来处理请求的结果，
          new SendCallback() {
            //投递成功
            public void onSuccess(SendResult sendResult) {
              System.out.println(sendResult);
            }
            //投递失败
            public void onException(Throwable throwable) {
              throwable.printStackTrace();
            }
          });
    }
  }

}
